package com.xiong.xtools.photo;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.xiong.xtools.R;
import com.xiong.xtools.mainstart.XImageUtils;
import com.xiong.xtools.mainstart.XPhotoUtils;
import com.xiong.xtools.mainstart.XSPUtils;
import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropActivity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
/**
 * Author : 熊文强
 * Time : 2017/9/28
 * Content : 圆形头像剪裁工具
 */
public class PhotoActivity extends AppCompatActivity {
	private PhotoActivity mContext;
	private ImageView mIvAvatar;
	private Uri resultUri;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo);
		mContext = PhotoActivity.this;
		mIvAvatar = (ImageView) findViewById(R.id.iv_avatar);
		initView();
	}

	protected void initView() {
		Resources r = mContext.getResources();
		resultUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
				+ r.getResourcePackageName(R.mipmap.ic_setting_user) + "/"
				+ r.getResourceTypeName(R.mipmap.ic_setting_user) + "/"
				+ r.getResourceEntryName(R.mipmap.ic_setting_user));

		mIvAvatar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				initDialogChooseImage();
			}
		});

		mIvAvatar.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View view) {
				XImageUtils.showBigImageView(mContext, resultUri);
				return false;
			}
		});
	}

	private void initDialogChooseImage() {
		RxDialogChooseImage dialogChooseImage = new RxDialogChooseImage(mContext, RxDialogChooseImage.LayoutType.TITLE);
		dialogChooseImage.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case XPhotoUtils.GET_IMAGE_FROM_PHONE://选择相册之后的处理
				if (resultCode == RESULT_OK) {
//                    RxPhotoUtils.cropImage(ActivityUser.this, );// 裁剪图片
					initUCrop(data.getData());
				}

				break;
			case XPhotoUtils.GET_IMAGE_BY_CAMERA://选择照相机之后的处理
				if (resultCode == RESULT_OK) {
                   /* data.getExtras().get("data");*/
//                    RxPhotoUtils.cropImage(ActivityUser.this, RxPhotoUtils.imageUriFromCamera);// 裁剪图片
					initUCrop(XPhotoUtils.imageUriFromCamera);
				}

				break;
			case XPhotoUtils.CROP_IMAGE://普通裁剪后的处理
				Glide.with(mContext).
						load(XPhotoUtils.cropImageUri).
						diskCacheStrategy(DiskCacheStrategy.RESULT).
						bitmapTransform(new CropCircleTransformation(mContext)).
						thumbnail(0.5f).
						placeholder(R.mipmap.ic_setting_user).
						priority(Priority.LOW).
						error(R.mipmap.ic_setting_user).
						fallback(R.mipmap.ic_setting_user).
						dontAnimate().
						into(mIvAvatar);
//                RequestUpdateAvatar(new File(RxPhotoUtils.getRealFilePath(mContext, RxPhotoUtils.cropImageUri)));
				break;

			case UCrop.REQUEST_CROP://UCrop裁剪之后的处理
				if (resultCode == RESULT_OK) {
					resultUri = UCrop.getOutput(data);
					roadImageView(resultUri, mIvAvatar);
					XSPUtils.putContent(mContext, "AVATAR", resultUri.toString());
				} else if (resultCode == UCrop.RESULT_ERROR) {
					final Throwable cropError = UCrop.getError(data);
				}
				break;
			case UCrop.RESULT_ERROR://UCrop裁剪错误之后的处理
				final Throwable cropError = UCrop.getError(data);
				break;
			default:
				break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	//从Uri中加载图片 并将其转化成File文件返回
	private File roadImageView(Uri uri, ImageView imageView) {
		Glide.with(mContext).
				load(uri).
				diskCacheStrategy(DiskCacheStrategy.RESULT).
				bitmapTransform(new CropCircleTransformation(mContext)).
				thumbnail(0.5f).
				placeholder(R.mipmap.ic_setting_user).
				priority(Priority.LOW).
				error(R.mipmap.ic_setting_user).
				fallback(R.mipmap.ic_setting_user).
				dontAnimate().
				into(imageView);

		return (new File(XPhotoUtils.getImageAbsolutePath(this, uri)));
	}

	private void initUCrop(Uri uri) {
		//Uri destinationUri = RxPhotoUtils.createImagePathUri(this);

		SimpleDateFormat timeFormatter = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA);
		long time = System.currentTimeMillis();
		String imageName = timeFormatter.format(new Date(time));

		Uri destinationUri = Uri.fromFile(new File(getCacheDir(), imageName + ".jpeg"));

		UCrop.Options options = new UCrop.Options();
		//设置裁剪图片可操作的手势
		options.setAllowedGestures(UCropActivity.SCALE, UCropActivity.ROTATE, UCropActivity.ALL);
		//设置隐藏底部容器，默认显示
		//options.setHideBottomControls(true);
		//设置toolbar颜色
		options.setToolbarColor(ActivityCompat.getColor(this, R.color.colorPrimary));
		//设置状态栏颜色
		options.setStatusBarColor(ActivityCompat.getColor(this, R.color.colorPrimaryDark));

		//开始设置
		//设置最大缩放比例
		options.setMaxScaleMultiplier(5);
		//设置图片在切换比例时的动画
		options.setImageToCropBoundsAnimDuration(666);
		//设置裁剪窗口是否为椭圆
//        options.setOvalDimmedLayer(true);
		//设置是否展示矩形裁剪框
//        options.setShowCropFrame(false);
		//设置裁剪框横竖线的宽度
//        options.setCropGridStrokeWidth(20);
		//设置裁剪框横竖线的颜色
//        options.setCropGridColor(Color.GREEN);
		//设置竖线的数量
//        options.setCropGridColumnCount(2);
		//设置横线的数量
//        options.setCropGridRowCount(1);

		UCrop.of(uri, destinationUri)
				.withAspectRatio(1, 1)
				.withMaxResultSize(1000, 1000)
				.withOptions(options)
				.start(PhotoActivity.this);
	}
}
